package com.hyyt.shopjsp.home.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.home.pojo.ShopHomeDayRecommend;
import com.hyyt.shopjsp.home.service.IShopHomeDayRecommendService;
import com.hyyt.shopjsp.product.vo.AllProductTypeVo;
import com.hyyt.shopjsp.product.pojo.ShopProductType;
import com.hyyt.shopjsp.product.pojo.ShopProductInfo;
import com.hyyt.shopjsp.product.service.IShopProductTypeService;
import com.hyyt.shopjsp.product.service.IShopProductInfoService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/03/22 0019下午 3:36.
 */
@Controller
@RequestMapping("/back/dayRecommend")
public class ShopHomeDayrecommendController extends BaseController {
    private Logger logger = Logger.getLogger(ShopHomeDayrecommendController.class);
    private String basePath = "/back/shop/dayRecommend/";

    @Resource
    private IShopHomeDayRecommendService shopHomeDayRecommendService;//首页每日推荐service
    @Resource
    private IShopProductTypeService shopProductTypeService;//商品分类service
    @Resource
    private IShopProductInfoService shopProductInfoService;//商品service

	/**
	 * 进入首页每日推荐管理
	 * @param param
	 * @return
	 */
	@RequestMapping("gotoDayRecommendPage")
	public String gotoDayRecommendPage(Map<String, Object> param){
		List<AllProductTypeVo> productTypeVos = RedisDataReadUtil.getProductType();
		param.put("categorys", productTypeVos);
		return basePath+"listDayRecommend";
	}

	/**
	 * 查询所有首页每日推荐
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="listDayRecommend",method=RequestMethod.POST)
	@ResponseBody
	public void listDayRecommend(String productName, String productCode, String isShow,Page page, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(productName)) {//商品名称
			param.put("productName", productName.trim());
		}
		if (Utils.objectIsNotEmpty(productCode)) {//商品编码
			param.put("productCode", productCode.trim());
		}
		if (Utils.objectIsNotEmpty(isShow)) {//显示状态
			param.put("isShow", isShow);
		}
		param.put("sortColumns", "id desc");
		int totalRecordCount =shopHomeDayRecommendService.countShopHomeDayRecommend(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopHomeDayRecommend> list =shopHomeDayRecommendService.queryShopHomeDayRecommend(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据首页每日推荐id获得对象
     *
     * @param id
     */
    @RequestMapping(value = "getDayRecommendInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getDayRecommendInfo(Integer id) {
        if (Utils.objectIsNotEmpty(id)) {
            ShopHomeDayRecommend dayRecommend = shopHomeDayRecommendService.loadShopHomeDayRecommend(id);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(dayRecommend));
        }else {
			return ResultMsg.create(false);
		}
    }

    /**
     * 保存或者修改首页每日推荐信息
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "saveOrUpdateDayRecommend", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateDayRecommend(Integer productId,ShopHomeDayRecommend dayRecommend) {
       if (Utils.objectIsNotEmpty(productId)) {
		   dayRecommend.setProductId(productId);
		   dayRecommend.setIsShow(1);
		   dayRecommend.setSort(0);
		   shopHomeDayRecommendService.saveOrUpdateDayRecommend(dayRecommend);
        }else{
		   shopHomeDayRecommendService.updateShopHomeDayRecommend(dayRecommend);
        }
        return ResultMsg.create(true);
    }

    /**
     * 根据传入的首页每日推荐id
     *
     * @param ids
     */
    @RequestMapping(value = "deleteDayRecommend", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteDayRecommend(String ids) {
        if (Utils.objectIsNotEmpty(ids)) {
        	shopHomeDayRecommendService.deleteShopHomeDayRecommend(Integer.parseInt(ids));
			return ResultMsg.create(true);
        }else {
			return ResultMsg.create(false);
		}
    }

    /**
     * 根据商品分类查询对应的商品
     * @param typeId
     * @param page
     * @param response
     */
    @RequestMapping(value="listProductByType",method=RequestMethod.POST)
    @ResponseBody
    public void listProductByType(String typeId,String productFullName,String productNo,Page page, HttpServletResponse response){
    	Map<String, Object> param=new HashMap<>();
		if(Utils.objectIsNotEmpty(typeId)){
			String typeIds = getTypeIdListByTypeId(typeId)+typeId;
			param.put("typeIds", typeIds);
		}else{
			typeId="4";
			String typeIds = getTypeIdListByTypeId(typeId)+typeId;
			param.put("typeIds", typeIds);
		}
		if(Utils.objectIsNotEmpty(productNo)){//商品编号
			param.put("productNo", productNo);
		}
		if(Utils.objectIsNotEmpty(productFullName)){//商品名称
			param.put("productFullName", productFullName);
		}
		int totalRecordCount=shopProductInfoService.countProductInfo(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopProductInfo> productInfoList=shopProductInfoService.queryListProductInfo(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", productInfoList);// rows键 存放每页记录 list
    	ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 供listProductByType调用
     * @param typeIdParams
     * @return
     */
    private String getTypeIdListByTypeId(String typeIdParams){
    	Map<String, Object> params=new HashMap<>();
    	String typeIds = typeIdParams+",";
    	params.put("parentId", typeIdParams);
    	List<ShopProductType> productTypeList=shopProductTypeService.queryShopProductType(params);
    	if(Utils.collectionIsNotEmpty(productTypeList)){
			for(ShopProductType pt : productTypeList){
				typeIds += getTypeIdListByTypeId(String.valueOf(pt.getProductTypeId()));
			}
		}
    	return typeIds;
    }
}
